define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order_report/index' + location.search,
                    add_url: 'order/order_report/add',
                    edit_url: 'order/order_report/edit',
                    del_url: 'order/order_report/del',
                    download_url: 'order/order_report/downloadAll',
                    multi_url: 'order/order_report/multi',
                    import_url: 'order/order_report/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true,formatter:function(value,row,index){
                            if(row.report_status != 30){
                                return {disabled:true};
                            }else{
                                return {disabled:false};
                            }
                        }},
                        {field: 'id', title: __('Id'),operate:false},
                        // {field: 'order_master_id', title: __('Order_master_id')},
                        {field: 'order_goods_id',operate:false, title: '检测项目',formatter:function(value,row,index){
                            return row.ordergoods?.map(item=>item.title).join(',') || '-'
                        }},
                        {field: 'user.nickname', title: '开单医生',formatter:function(value,row,index){
                            return `<div>姓名:${row.user?.nickname}</div><div>手机号:${row.user?.mobile}</div>`
                        }},
                        {field: 'sole_id', title: '销售',formatter:function(value,row,index){
                            return `<div>姓名:${row.sale?.nickname}</div><div>手机号:${row.sale?.mobile}</div>`
                        }},
                        {field: 'order_tube', title: '试管',formatter:function(value,row,index){
                            let html = '';
                            row.order_tube.forEach(item=>{
                                html += `<div>${item.biaoben_rongqi_name}：${item.tube_number}</p>
                            </div>`
                            })
                            return html
                             
                        }},
                        {field: 'hospital_id', title: '医院',formatter:function(value,row,index){
                            return row.hospital_name
                        },searchList:$.getJSON('hospital/hospital/getJson')},
                        // {field: 'order_type', title: __('Order_type'), searchList: {"10":__('Order_type 10'),"20":__('Order_type 20')}, formatter: Table.api.formatter.normal},
                        // {field: 'goods_package_id', title: __('Goods_package_id')},
                        {field: 'username', title: __('Username'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'userphone', title: __('Userphone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'age', title: __('Age'),operate:false},
                        {field: 'gender', title: __('Gender'), operate:false},
                        // {field: 'pay_type', title: __('Pay_type'), searchList: {"10":__('Pay_type 10'),"20":__('Pay_type 20')}, formatter: Table.api.formatter.normal},
                        {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'transaction_id', title: __('Transaction_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'out_refund_no', title: __('Out_refund_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'refund_id', title: __('Refund_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'order_sn', title: __('Order_sn'),operate:false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'price', title: __('Price'), operate:false},
                        // {field: 'original_price', title: __('Original_price'), operate:false},
                        // {field: 'pay_status', title: __('Pay_status'), searchList: {"0":__('Pay_status 0'),"1":__('Pay_status 1')}, formatter: Table.api.formatter.status},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.status', title: __('Status'), searchList: {"0":__('Status 0'),"10":__('Status 10'),"20":__('Status 20'),"30":__('Status 30'),"-10":__('Status -10'),"-20":__('Status -20')}, formatter: function(value,row,index){
                        //     return Table.api.formatter.status.call(this,row.status,row,index)
                        // }},
                        {field: 'report_status', title: __('Report_status'), searchList: {"0":__('Report_status 0'),"10":__('Report_status 10'),"20":__('Report_status 20'),"25":"部分完成","30":__('Report_status 30')}, formatter: Table.api.formatter.status},
                        {field: 'download_report_status', title: __('下载状态'), operate: false, searchList: {"10":__('未下载'),"20":__('部分下载'),"30":__('全部下载')}, formatter: Table.api.formatter.status},
                        // {field: 'report_image', title: __('Report_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'quyangtime', title: __('Quyangtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'songjiantime', title: __('Songjiantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'overtime', title: __('Overtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'quxiaotime', title: __('Quxiaotime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'tuikuantime', title: __('Tuikuantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'buttons', title: '操作', table: table, events: Table.api.events.operate, buttons:[
                            {
                                name: 'detail',
                                text: __('报告'),
                                classname: 'btn btn-xs btn-primary',
                                icon: 'fa fa-download',
                                url: 'order/order_report/downloadAll',
                                callbacks: function (data,row,index) {
                                    // console.log(row.report_image);
                                    // Table.api.formatter.images.call(this,row.report)
                                },
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    return row.report_status == 30;
                                }
                            },
                            {
                                name: 'detail',
                                text: __('查看报告'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                url: 'order/order_report/detail',
                                callbacks: function (data,row,index) {
                                    // console.log(row.report_image);
                                    // Table.api.formatter.images.call(this,row.report_image,row,index)
                                },
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    return row.report_status == 30;
                                }
                            },
                        ],formatter: Table.api.formatter.buttons}
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-download", function () {
                var ids = Table.api.selectedids(table);
                window.location.href = Fast.api.fixurl("order/order_report/downloadAll/ids/"+ids.join(","));
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
            $(document).on("click", ".img-center", function () {
                var data = [];
                var arr = $(this).attr('data-list')?$(this).attr('data-list').split(","):[];
                var url;
                $.each(arr, function (index, value) {
                    url = Fast.api.cdnurl(value);
                    data.push({
                        src: url,
                        thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
                    });
                });
                Layer.photos({
                    photos: {
                        "start": $(this).parent().index(),
                        "data": data
                    },
                    shade: 0.4,
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
